<?php
class ershoufang extends base{

	public function __construct(){
		parent::__construct();

		$this->cityObj = D('city');
		$this->cateObj = D('cate');

		$this->ershoufangObj = D('ershoufang');
	}

	public function setting(){

		$city_id = arg('city_id');

		$areas = $this->cityObj->get_citys($city_id);

		$cates = $this->cateObj->get_cates(1);
		
		$data['areas'] = $areas;
		$data['cates'] = $cates;
		// dump($data);exit;
		$this->renderSuccess($data);
		
	}


	public function add(){

		if($this->fmdo == 'add'){

			$formData = arg('formData');
			$formData = json_decode($formData,true);

			$arrBase = array(
				'city_id' => $formData['city_id'],
				'cate_id' => $formData['cate_id'],
				'house_xiaoqu' => $formData['house_xiaoqu'],
				'house_area' => $formData['house_area'],
				'house_price' => $formData['house_price'],
				'intro' => $formData['city_id'],
				'photo' => joinStr(arg('photo')),
				'add_time' => date('Y-m-d H:i:s'),
				'user_id' => arg('user_id'),
			);

			dump($arrBase);exit;
			$house_id = $this->ershoufangObj->add($arrBase);
			if($house_id){

			}
			$this->renderSuccess('发布成功');
		}
		
	}



	public function index(){

		$result = $this->cityObj->findAll(array("status=1 and sort=1"),'level asc');
		if($result){
			foreach ($result as $key => $val) {
				$areas[$val['first']][] = $val;
			}
		}
		// dump($areas);exit;
		//区
		for($i=ord('A'); $i<=ord('Z'); $i++){
			$zm = chr($i);
			if($areas[$zm]){
				$citys[$zm] = $areas[$zm];
				$zimu[] = $zm;
			}
		}

		$data['zimu'] = $zimu;
		$data['citys'] = $citys;
		// dump($data);exit;
		$this->renderSuccess($data);
		
	}

	public function nowcity() {

		$lng = arg('lng');
		$lat = arg('lat');
		$result = $this->cityObj->findAll(array("status=1 and sort=1"),'level asc');
		if($result){
			foreach ($result as $key => $val) {
				$area_ids[] = $val['id'];
			}
			$area_ids = implode(',', $area_ids);

			if($lng && $lat){
				$cond = "status=1 and id IN({$area_ids})";
				$sort = " (ABS(lng - '{$lng}') +  ABS(lat - '{$lat}') ) asc ";

				$area = $this->cityObj->find(array($cond),$sort);

				$data['city'] = $area;
				// dump($data);exit;
				$this->renderSuccess($data);
			}
			
		}

	}

}